package org.ytor.core.sysapi.role.api;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import org.ytor.common.exception.BaseException;
import org.ytor.common.model.R;
import org.ytor.common.util.Strs;
import org.ytor.common.util.bean.Beans;
import org.ytor.core.BaseController;
import org.ytor.core.querygen.WhereGenerator;
import org.ytor.core.sqlflow.Page;
import org.ytor.core.sqlflow.builder.support.WhereBuilder;
import org.ytor.core.sysapi.role.req.SysRolePermissionReq;
import org.ytor.core.sysapi.role.resp.SysRolePermissionResp;
import org.ytor.core.sysapi.role.logic.SysRoleLogic;
import org.ytor.core.sysapi.role.model.SysRole;
import org.ytor.core.sysapi.role.repository.SysRoleRepository;
import org.ytor.core.sysapi.role.req.SysRoleReq;
import org.ytor.core.sysapi.role.resp.SysRoleResp;
import org.ytor.core.util.Pages;

import java.util.Arrays;
import java.util.List;

/**
 * created by   on 2025年4月13日 16:01:41
 * <br/>
 * SysRole表控制器
 * <br/>
 */
@RestController
@RequestMapping("/rbac/sysRole")
@Tag(name = "角色表")
public class SysRoleController extends BaseController<SysRole, SysRoleLogic, SysRoleRepository> {

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询", description = "根据id查询对应的实体数据数据")
    public Page<SysRoleResp> page(@ParameterObject SysRoleReq roleReq,
                                  @RequestParam(defaultValue = "1") Integer pageNo,
                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        WhereBuilder where = WhereGenerator.where();
        Page<SysRole> page = repository.queryPage(pageNo, pageSize, where);
        return Pages.transPage(page, SysRole::toResp);
    }

    /**
     * 根据id查询数据
     */
    @GetMapping("/queryById")
    @Operation(summary = "根据id查询", description = "根据id查询数据")
    protected R<SysRoleResp> queryById(String id) {
        SysRole sysRole = repository.queryById(id);
        return R.success(sysRole.toResp());
    }

    /**
     * 新增或编辑数据
     */
    @PostMapping("/addOrUpdate")
    @Operation(summary = "新增或编辑", description = "根据有无主键id,新增或编辑数据")
    protected R<String> addOrUpdate(@RequestBody SysRoleReq sysRoledata) {
        SysRole sysRole = Beans.copyProperties(sysRoledata, SysRole.class);
        if ("-".equals(sysRole.getRoleDepartCode())) {
            sysRole.setRoleDepartCode(null);
        }
        repository.insertOrUpdate(sysRole);
        return R.success(sysRoledata.getId() == null ? "新增成功" : "编辑成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除", description = "根据id删除数据")
    protected R<String> delete(@Parameter(description = "主键id") String ids) {
        if (Strs.isEmpty(ids)) {
            throw new BaseException("删除接口的ids不能为空");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        repository.deleteById(idList);
        return R.success("删除成功");
    }

    /**
     * 查询角色授权信息
     */
    @GetMapping("/listRolePermissions")
    @Operation(summary = "查询角色授权信息", description = "查询角色授权信息")
    public List<SysRolePermissionResp> listRolePermissions(@RequestParam String roleId) {
        return logic.listRolePermissions(roleId);
    }

    /**
     * 修改角色授权信息
     */
    @PostMapping("/updateRolePermissions")
    @Operation(summary = "修改角色授权信息", description = "修改角色授权信息")
    public R<String> updateRolePermissions(@RequestBody SysRolePermissionReq rolePermissionReq) {
        logic.updateRolePermissions(rolePermissionReq);
        return R.success("授权成功");
    }

}